﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CAGFIP.Tools
{
    public static class Shuffler
    {
        #region Fields

        static Random r;

        #endregion

        #region Constructors
        
        static Shuffler()
        {
            r = new Random();
        }

        #endregion

        #region Methods

        //Durstenfeld's algorithm: http://en.wikipedia.org/wiki/Knuth_shuffle
        public static void Shuffle<T>(List<T> elements)
        {
            for (int i = elements.Count; i > 1; i--)
            {
                int j = r.Next(i);
                T aux = elements[j];
                elements[j] = elements[i - 1];
                elements[i - 1] = aux;
            }
        }

        #endregion

    }
}
